import java.util.HashMap;

public class Solution166 {
    public String fractionToDecimal(int numerator, int denominator) {
        StringBuilder res = new StringBuilder();
        HashMap<Long, Integer> map = new HashMap<>();
        long numeratorL = numerator;
        int i;
        if ((double) numeratorL / (double) denominator < 0) {
            res.append('-');
        }
        if (numeratorL > 0) {
            numeratorL = -numeratorL;
        }
        if (denominator > 0) {
            denominator = -denominator;
        }
        res.append(numeratorL / denominator);
        numeratorL %= denominator;
        if (numeratorL == 0) {
            return res.toString();
        }
        res.append('.');
        i = res.length();
        while (numeratorL != 0) {
            numeratorL *= 10;
            if (map.containsKey(numeratorL)) {
                int pos = map.get(numeratorL);
                res.insert(pos, '(').append(')');
                return res.toString();
            } else {
                map.put(numeratorL, i);
            }
            res.append(numeratorL / denominator);
            numeratorL %= denominator;
            i++;
        }
        return res.toString();
    }
}
